package Offer.offer2019;

import java.util.Stack;

/**
 * 栈的压入 弹出序列
 *
 * @author: leiwenkang02@meituan.com
 * @date: 2019/5/7
 * @time: 12:20
 */
public class Offer22 {
    public static void main(String[] args) {
        Offer22 offer = new Offer22();
        int[] pushArr = {1, 2, 3, 4, 5};
        int[] popArray = {4, 5, 3, 2, 1};
        System.out.println(offer.isPopOrder(pushArr, popArray));
    }

    public boolean isPopOrder(int[] pushArr, int[] popArray) {
        if (pushArr == null || pushArr.length == 0 || popArray == null || popArray.length == 0) {
            throw new RuntimeException("非法输入！");
        }
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushArr.length; i++) {
            stack.push(pushArr[i]);
            while (j < popArray.length && stack.peek() == popArray[j]) {
                stack.pop();
                j++;
            }

        }
        return stack.isEmpty();
    }
}
